package com.ruoyi.common.order.strategy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 策略工厂
 */
@Component
public class OrderNumberStrategyFactory {

    /**
     * 容器
     */
    private final Map<String, OrderNumberStrategy> content = new HashMap<>();

    /**
     * 填充容器
     * @param orderNumberStrategies 策略接口的实现类们
     */
    @Autowired
    public OrderNumberStrategyFactory(List<OrderNumberStrategy> orderNumberStrategies) {
        orderNumberStrategies.forEach(orderNumberStrategy
                -> content.put(orderNumberStrategy.identity(), orderNumberStrategy));
    }

    /**
     * 获取执行器
     * @param code
     * @return
     */
    public OrderNumberStrategy queryExecute(String code) {
        OrderNumberStrategy orderNumberStrategy = content.get(code);
        if(null == orderNumberStrategy) {
            throw new RuntimeException("不支持的订单类型: " + code);
        }
        return orderNumberStrategy;
    }
}
